﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAO;
using DTO;
using System.Xml;

namespace BUS
{
    public class SearchEngine
    {
        #region Constants
        private const int FILTER_BY_BOTH = 0;
        private const int FILTER_BY_DUONG_ONLY = 1;
        private const int FILTER_BY_DICHVU_ONLY = 2;
        private const int FILTER_BY_NONE = 3;

        public const int FILTER_INFO_DUONG = 0;
        public const int FILTER_INFO_DICHVU = 1;
        private const string TAT_CA_DUONG = "Tất cả đường";
        private const string TAT_CA_DICHVU = "Tất cả dịch vụ";
        #endregion

        # region Fields
        private DichVuDAO _dichVuDAO = null;
        private DuLieuDAO _duLieuDAO = null;
        private DuongDAO _duongDAO = null;
        private PhuongDAO _phuongDAO = null;
        private QuanHuyenDAO _quanHuyenDAO = null;
        private TenDiaDiemDAO _tenDiaDiemDAO = null;
        private TinhThanhDAO _tinhThanhDAO = null;
        private TuKhoaDiaDiemDAO _tuKhoaDiaDiemDAO = null;
        private TuKhoaDichVuDAO _tuKhoaDichVuDAO = null;
        private TuKhoaDuongDAO _tuKhoaDuongDAO = null;
        private TuKhoaPhuongDAO _tuKhoaPhuongDAO = null;
        private TuKhoaQuanHuyenDAO _tuKhoaQuanHuyenDAO = null;
        private TuKhoaTinhThanhDAO _tuKhoaTinhThanhDAO = null;

        private List<SearchResultObject> _lstSearchResult = null;

        // danh cho filter - danh sach DUONG, PHUONG, QUANHUYEN tren bo nho
        private SortedList<int, string> _slstDuongDTO = null;
        private SortedList<int, string> _slstDichVuDTO = null;
        #endregion

        public SearchEngine()
        {
            try
            {
                this._dichVuDAO = new DichVuDAO();
                this._duLieuDAO = new DuLieuDAO();
                this._duongDAO = new DuongDAO();
                this._phuongDAO = new PhuongDAO();
                this._quanHuyenDAO = new QuanHuyenDAO();
                this._tenDiaDiemDAO = new TenDiaDiemDAO();
                this._tinhThanhDAO = new TinhThanhDAO();
                this._tuKhoaDiaDiemDAO = new TuKhoaDiaDiemDAO();
                this._tuKhoaDichVuDAO = new TuKhoaDichVuDAO();
                this._tuKhoaDuongDAO = new TuKhoaDuongDAO();
                this._tuKhoaPhuongDAO = new TuKhoaPhuongDAO();
                this._tuKhoaQuanHuyenDAO = new TuKhoaQuanHuyenDAO();
                this._tuKhoaTinhThanhDAO = new TuKhoaTinhThanhDAO();

                this._lstSearchResult = new List<SearchResultObject>();

                // nap danh sach DUONG, PHUONG, QUANHUYEN vao bo nho
                List<DichVuDTO> lstDichVuDTO = this.GetAllDichVuDTO();
                List<DuongDTO> lstDuongDTO = this.GetAllDuongDTO();

                this._slstDichVuDTO = new SortedList<int, string>();
                foreach (DichVuDTO dichVuDTO in lstDichVuDTO)
                {
                    this._slstDichVuDTO.Add(dichVuDTO.MaDichVu, dichVuDTO.TenDichVu);
                }

                this._slstDuongDTO = new SortedList<int, string>();
                foreach (DuongDTO duongDTO in lstDuongDTO)
                {
                    this._slstDuongDTO.Add(duongDTO.MaDuong, duongDTO.TenDuong);
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public List<DichVuDTO> GetAllDichVuDTO()
        {
            List<DichVuDTO> lstDichVuDTO = null;
            try
            {
                DataProvider.Connect();
                lstDichVuDTO = this._dichVuDAO.GetAllDichVuDTO();
            }
            catch (System.Exception ex)
            {
                lstDichVuDTO = new List<DichVuDTO>();
                throw ex;
            }
            finally
            {
                DataProvider.Disconnect();
            }

            return lstDichVuDTO;
        }

        public List<QuanHuyenDTO> GetAllQuanHuyenDTO()
        {
            List<QuanHuyenDTO> lstQuanHuyenDTO = null;
            try
            {
                DataProvider.Connect();
                lstQuanHuyenDTO = this._quanHuyenDAO.GetAllQuanHuyenDTO();
            }
            catch (System.Exception ex)
            {
                lstQuanHuyenDTO = new List<QuanHuyenDTO>();
                throw ex;
            }
            finally
            {
                DataProvider.Disconnect();
            }

            return lstQuanHuyenDTO;
        }

        public List<PhuongDTO> GetAllPhuongDTO()
        {
            List<PhuongDTO> lstPhuongDTO = null;
            try
            {
                DataProvider.Connect();
                lstPhuongDTO = this._phuongDAO.GetAllPhuongDTO();
            }
            catch (System.Exception ex)
            {
                lstPhuongDTO = new List<PhuongDTO>();
                throw ex;
            }
            finally
            {
                DataProvider.Disconnect();
            }

            return lstPhuongDTO;
        }

        public List<DuongDTO> GetAllDuongDTO()
        {
            List<DuongDTO> lstDuongDTO = null;
            try
            {
                DataProvider.Connect();
                lstDuongDTO = this._duongDAO.GetAllDuongDTO();
            }
            catch (System.Exception ex)
            {
                lstDuongDTO = new List<DuongDTO>();
                throw ex;
            }
            finally
            {
                DataProvider.Disconnect();
            }

            return lstDuongDTO;
        }

        public List<DuLieuDTO> Search(string strInput, int maDichVu)
        {
            List<DuLieuDTO> lstResult = new List<DuLieuDTO>();

            try
            {
                // xoa ket qua cu
                if (null != this._lstSearchResult)
                {
                    this._lstSearchResult.Clear();
                }
                else
                {
                    this._lstSearchResult = new List<SearchResultObject>();
                }

                // tao ket noi voi CSDL
                //if(_duLieuDAO.getcon().State==System.Data.ConnectionState.Closed)
                DataProvider.Connect();

                // tim kiem
                strInput = Utilities.FormatInputData(strInput);
                if (strInput.Length > 0)
                {
                    bool isExistNumber = Utilities.CheckExistNumber(strInput);
                    if (isExistNumber)
                    {
                        List<DuLieuDTO> lstDuLieuDTO = this._duLieuDAO.SearchWithSoNha(strInput);

                        lstDuLieuDTO = lstDuLieuDTO.RemoveDupicatesOnSoNha();

                        int nDuLieuDTO = lstDuLieuDTO.Count;
                        if (nDuLieuDTO > 0)
                        {
                            string strTemp = String.Empty;
                            for (int i = 0; i < nDuLieuDTO; i++)
                            {
                                strTemp = strInput.Clear(lstDuLieuDTO[i].SoNha);
                                if (strTemp.Length > 0)
                                {
                                    this.SearchTuKhoaDichVu(strTemp, lstDuLieuDTO[i].SoNha, maDichVu);
                                }
                                else
                                {
                                    SearchResultObject sr = new SearchResultObject();
                                    sr.SoNha = lstDuLieuDTO[i].SoNha;
                                    if (0 != maDichVu)
                                    {
                                        sr.MaDichVu = maDichVu;
                                    }

                                    this._lstSearchResult.Add(sr);
                                }
                            }
                        }

                        this.SearchTuKhoaDichVu(strInput, String.Empty, maDichVu);                       
                    }
                    else
                    {
                        this.SearchTuKhoaDichVu(strInput, String.Empty, maDichVu);
                    }

                    List<DuLieuDTO> lstTemp = null;
                    int nSearchResult = this._lstSearchResult.Count;
                    for (int i = 0; i < nSearchResult; i++)
                    {
                        lstTemp = this._duLieuDAO.Search(
                            this._lstSearchResult[i].SoNha,
                            this._lstSearchResult[i].MaDichVu,
                            this._lstSearchResult[i].MaTinhThanh,
                            this._lstSearchResult[i].MaQuanHuyen,
                            this._lstSearchResult[i].MaPhuong,
                            this._lstSearchResult[i].MaDuong,
                            this._lstSearchResult[i].MaTenDiaDiem
                            );
                        lstResult.AddRange(lstTemp);
                    }
                }
                else
                {
                    if (0 != maDichVu)
                    {
                        this.SearchTuKhoaDichVu(strInput, String.Empty, maDichVu);
                        List<DuLieuDTO> lstTemp = null;
                        int nSearchResult = this._lstSearchResult.Count;
                        for (int i = 0; i < nSearchResult; i++)
                        {
                            lstTemp = this._duLieuDAO.Search(
                                this._lstSearchResult[i].SoNha,
                                this._lstSearchResult[i].MaDichVu,
                                this._lstSearchResult[i].MaTinhThanh,
                                this._lstSearchResult[i].MaQuanHuyen,
                                this._lstSearchResult[i].MaPhuong,
                                this._lstSearchResult[i].MaDuong,
                                this._lstSearchResult[i].MaTenDiaDiem
                                );
                            lstResult.AddRange(lstTemp);
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                lstResult = new List<DuLieuDTO>();
                throw ex;
            }
            finally
            {
                if (_duLieuDAO.getcon().State == System.Data.ConnectionState.Open)
                DataProvider.Disconnect();
            }

            return lstResult;
        }

        private void SearchTuKhoaDichVu(string strInput, string soNha, int maDichVu)
        {
            if (0 == maDichVu) // tim tat ca dich vu
            {
                List<TuKhoaDichVuDTO> lstTuKhoaDichVuDTO = this._tuKhoaDichVuDAO.Search(strInput);
                int nTuKhoaDichVuDTO = lstTuKhoaDichVuDTO.Count;
                if (nTuKhoaDichVuDTO > 0)
                {
                    string strTemp = String.Empty;
                    for (int i = 0; i < nTuKhoaDichVuDTO; i++)
                    {
                        strTemp = strInput.Clear(lstTuKhoaDichVuDTO[i].TuKhoaDichVu);
                        if (strTemp.Length > 0)
                        {
                            this.SearchTuKhoaTinhThanh(strTemp, soNha, lstTuKhoaDichVuDTO[i].MaDichVu);
                        }
                        else
                        {
                            SearchResultObject sr = new SearchResultObject();
                            sr.SoNha = soNha;
                            sr.MaDichVu = lstTuKhoaDichVuDTO[i].MaDichVu;
                            this._lstSearchResult.Add(sr);
                        }
                    }
                }

                this.SearchTuKhoaTinhThanh(strInput, soNha, DataProvider._defaultNumber);
            }
            else // co ma dich vu cu the
            {
                if (0 == strInput.Length)
                {
                    SearchResultObject sr = new SearchResultObject();
                    sr.SoNha = soNha;
                    sr.MaDichVu = maDichVu;
                    this._lstSearchResult.Add(sr);
                }
                else
                {
                    this.SearchTuKhoaTinhThanh(strInput, soNha, maDichVu);
                }
            }
        }

        private void SearchTuKhoaTinhThanh(string strInput, string soNha, int maDichVu)
        {
            List<TuKhoaTinhThanhDTO> lstTuKhoaTinhThanhDTO = this._tuKhoaTinhThanhDAO.Search(strInput);
            int nTuKhoaTinhThanhDTO = lstTuKhoaTinhThanhDTO.Count;
            if (nTuKhoaTinhThanhDTO > 0)
            {
                string strTemp = String.Empty;
                for (int i = 0; i < lstTuKhoaTinhThanhDTO.Count; i++)
                {
                    strTemp = strInput.Clear(lstTuKhoaTinhThanhDTO[i].TuKhoaTinhThanh);
                    if (strTemp.Length > 0)
                    {
                        this.SearchTuKhoaQuanHuyen(strTemp, soNha, maDichVu, lstTuKhoaTinhThanhDTO[i].MaTinhThanh);
                    }
                    else
                    {
                        SearchResultObject sr = new SearchResultObject();
                        sr.SoNha = soNha;
                        sr.MaDichVu = maDichVu;
                        sr.MaTinhThanh = lstTuKhoaTinhThanhDTO[i].MaTinhThanh;
                        this._lstSearchResult.Add(sr);
                    }
                }
            }

            this.SearchTuKhoaQuanHuyen(strInput, soNha, maDichVu, DataProvider._defaultNumber);
        }

        private void SearchTuKhoaQuanHuyen(string strInput, string soNha, int maDichVu, int maTinhThanh)
        {
            List<TuKhoaQuanHuyenDTO> lstTuKhoaQuanHuyenDTO = this._tuKhoaQuanHuyenDAO.Search(strInput);
            int nTuKhoaQuanHuyenDTO = lstTuKhoaQuanHuyenDTO.Count;
            if (nTuKhoaQuanHuyenDTO > 0)
            {
                string strTemp = String.Empty;
                for (int i = 0; i < lstTuKhoaQuanHuyenDTO.Count; i++)
                {
                    strTemp = strInput.Clear(lstTuKhoaQuanHuyenDTO[i].TuKhoaQuanHuyen);
                    if (strTemp.Length > 0)
                    {
                        this.SearchTuKhoaPhuong(strTemp, soNha, maDichVu, maTinhThanh, lstTuKhoaQuanHuyenDTO[i].MaQuanHuyen);
                    }
                    else
                    {
                        SearchResultObject sr = new SearchResultObject();
                        sr.SoNha = soNha;
                        sr.MaDichVu = maDichVu;
                        sr.MaTinhThanh = maTinhThanh;
                        sr.MaQuanHuyen = lstTuKhoaQuanHuyenDTO[i].MaQuanHuyen;
                        this._lstSearchResult.Add(sr);
                    }
                }
            }

            this.SearchTuKhoaPhuong(strInput, soNha, maDichVu, maTinhThanh, DataProvider._defaultNumber);
        }

        private void SearchTuKhoaPhuong(string strInput, string soNha, int maDichVu, int maTinhThanh, int maQuanHuyen)
        {
            List<TuKhoaPhuongDTO> lstTuKhoaPhuongDTO = this._tuKhoaPhuongDAO.Search(strInput);
            int nTuKhoaPhuongDTO = lstTuKhoaPhuongDTO.Count;
            if (nTuKhoaPhuongDTO > 0)
            {
                string strTemp = String.Empty;
                for (int i = 0; i < lstTuKhoaPhuongDTO.Count; i++)
                {
                    strTemp = strInput.Clear(lstTuKhoaPhuongDTO[i].TuKhoaPhuong);
                    if (strTemp.Length > 0)
                    {
                        this.SearchTuKhoaDuong(strTemp, soNha, maDichVu, maTinhThanh, maQuanHuyen, lstTuKhoaPhuongDTO[i].MaPhuong);
                    }
                    else
                    {
                        SearchResultObject sr = new SearchResultObject();
                        sr.SoNha = soNha;
                        sr.MaDichVu = maDichVu;
                        sr.MaTinhThanh = maTinhThanh;
                        sr.MaQuanHuyen = maQuanHuyen;
                        sr.MaPhuong = lstTuKhoaPhuongDTO[i].MaPhuong;
                        this._lstSearchResult.Add(sr);
                    }
                }
            }

            this.SearchTuKhoaDuong(strInput, soNha, maDichVu, maTinhThanh, maQuanHuyen, DataProvider._defaultNumber);
        }

        private void SearchTuKhoaDuong(string strInput, string soNha, int maDichVu, int maTinhThanh, int maQuanHuyen, int maPhuong)
        {
            List<TuKhoaDuongDTO> lstTuKhoaDuongDTO = this._tuKhoaDuongDAO.Search(strInput);
            int nTuKhoaDuongDTO = lstTuKhoaDuongDTO.Count;
            if (nTuKhoaDuongDTO > 0)
            {
                string strTemp = String.Empty;
                for (int i = 0; i < lstTuKhoaDuongDTO.Count; i++)
                {
                    strTemp = strInput.Clear(lstTuKhoaDuongDTO[i].TuKhoaDuong);
                    if (strTemp.Length > 0)
                    {
                        this.SearchTuKhoaDiaDiem(strTemp, soNha, maDichVu, maTinhThanh, maQuanHuyen, maPhuong, lstTuKhoaDuongDTO[i].MaDuong);
                    }
                    else
                    {
                        SearchResultObject sr = new SearchResultObject();
                        sr.SoNha = soNha;
                        sr.MaDichVu = maDichVu;
                        sr.MaTinhThanh = maTinhThanh;
                        sr.MaQuanHuyen = maQuanHuyen;
                        sr.MaPhuong = maPhuong;
                        sr.MaDuong = lstTuKhoaDuongDTO[i].MaDuong;
                        this._lstSearchResult.Add(sr);
                    }
                }
            }

            this.SearchTuKhoaDiaDiem(strInput, soNha, maDichVu, maTinhThanh, maQuanHuyen, maPhuong, DataProvider._defaultNumber);
        }

        private void SearchTuKhoaDiaDiem(string strInput, string soNha, int maDichVu, int maTinhThanh, int maQuanHuyen, int maPhuong, int maDuong)
        {
            List<TuKhoaDiaDiemDTO> lstTuKhoaDiaDiemDTO = this._tuKhoaDiaDiemDAO.Search(strInput);
            int nTuKhoaDiaDiemDTO = lstTuKhoaDiaDiemDTO.Count;
            if (nTuKhoaDiaDiemDTO > 0)
            {
                string strTemp = String.Empty;
                for (int i = 0; i < lstTuKhoaDiaDiemDTO.Count; i++)
                {
                    strTemp = strInput.Clear(lstTuKhoaDiaDiemDTO[i].TuKhoaTenDiaDiem);
                    if (0 == strTemp.Length)
                    {
                        SearchResultObject sr = new SearchResultObject();
                        sr.SoNha = soNha;
                        sr.MaDichVu = maDichVu;
                        sr.MaTinhThanh = maTinhThanh;
                        sr.MaQuanHuyen = maQuanHuyen;
                        sr.MaPhuong = maPhuong;
                        sr.MaDuong = maDuong;
                        sr.MaTenDiaDiem = lstTuKhoaDiaDiemDTO[i].MaTenDiaDiem;
                        this._lstSearchResult.Add(sr);
                    }
                }
            }
        }

        // ket bang
        public List<DuLieuThoDTO> Reference(List<DuLieuDTO> lstDuLieuDTO, int start, int end)
        {
            List<DuLieuThoDTO> lstDuLieuThoDTO = new List<DuLieuThoDTO>();
            try
            {
                DataProvider.Connect();

                DuLieuThoDTO duLieuThoDTO = null;
                TenDiaDiemDTO tenDiaDiemDTO = null;
                DuongDTO duongDTO = null;
                PhuongDTO phuongDTO = null;
                QuanHuyenDTO quanHuyenDTO = null;
                TinhThanhDTO tinhThanhDTO = null;
                for (int i = start; i < end; i++)
                {
                    if (i < 0 || i >= lstDuLieuDTO.Count)
                    {
                        break;
                    }

                    duLieuThoDTO = new DuLieuThoDTO();
                    DichVuDTO dichVuDTO = this._dichVuDAO.Search(lstDuLieuDTO[i].MaDichVu);
                    if (null != dichVuDTO)
                    {
                        duLieuThoDTO.TenDichVu = dichVuDTO.TenDichVu;
                    }

                    tenDiaDiemDTO = this._tenDiaDiemDAO.Search(lstDuLieuDTO[i].MaTenDiaDiem);
                    if (null != tenDiaDiemDTO)
                    {
                        duLieuThoDTO.TenDiaDiem = tenDiaDiemDTO.TenDiaDiem;
                    }

                    duLieuThoDTO.SoNha = lstDuLieuDTO[i].SoNha;
                    duongDTO = this._duongDAO.Search(lstDuLieuDTO[i].MaDuong);
                    if (null != duongDTO)
                    {
                        duLieuThoDTO.TenDuong = duongDTO.TenDuong;
                    }

                    phuongDTO = this._phuongDAO.Search(lstDuLieuDTO[i].MaPhuong);
                    if (null != phuongDTO)
                    {
                        duLieuThoDTO.TenPhuong = phuongDTO.TenPhuong;
                    }

                    quanHuyenDTO = this._quanHuyenDAO.Search(lstDuLieuDTO[i].MaQuanHuyen);
                    if (null != quanHuyenDTO)
                    {
                        duLieuThoDTO.TenQuanHuyen = quanHuyenDTO.TenQuanHuyen;
                    }

                    tinhThanhDTO = this._tinhThanhDAO.Search(lstDuLieuDTO[i].MaTinhThanh);
                    if (null != tinhThanhDTO)
                    {
                        duLieuThoDTO.TenTinhThanh = tinhThanhDTO.TenTinhThanh;
                    }

                    duLieuThoDTO.KinhDo = lstDuLieuDTO[i].KinhDo;
                    duLieuThoDTO.ViDo = lstDuLieuDTO[i].ViDo;
                    duLieuThoDTO.ChuThich = lstDuLieuDTO[i].ChuThich;

                    lstDuLieuThoDTO.Add(duLieuThoDTO);
                }
            }
            catch (Exception ex)
            {
                lstDuLieuThoDTO = new List<DuLieuThoDTO>();
                throw ex;
            }
            finally
            {
                DataProvider.Disconnect();
            }

            return lstDuLieuThoDTO;
        }

        public List<DuLieuThoDTO> Reference2(List<DuLieuDTO> lstDuLieuDTO, int start, int end)
        {
            List<DuLieuThoDTO> lstDuLieuThoDTO = new List<DuLieuThoDTO>();
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                DuLieuThoDTO duLieuThoDTO = null;
                for (int i = start; i < end; i++)
                {
                    if (i < 0 || i >= lstDuLieuDTO.Count)
                    {
                        break;
                    }

                    duLieuThoDTO = new DuLieuThoDTO();
                    try
                    {
                        xmlDoc.LoadXml(lstDuLieuDTO[i].ChuThich);
                    }
                    catch (System.Exception ex)
                    {
                        Utilities.WriteLog(ex.Message);
                        continue;
                    }

                    XmlNodeList xmlNodeList = xmlDoc.DocumentElement.ChildNodes;

                    if (10 == xmlNodeList.Count)
                    {
                        duLieuThoDTO.TenDichVu = xmlNodeList[0].InnerText;
                        duLieuThoDTO.TenDiaDiem = xmlNodeList[1].InnerText;
                        duLieuThoDTO.SoNha = xmlNodeList[2].InnerText;
                        duLieuThoDTO.TenDuong = xmlNodeList[3].InnerText;
                        duLieuThoDTO.TenPhuong = xmlNodeList[4].InnerText;
                        duLieuThoDTO.TenQuanHuyen = xmlNodeList[5].InnerText;
                        duLieuThoDTO.TenTinhThanh = xmlNodeList[6].InnerText;
                        try
                        {
                            duLieuThoDTO.KinhDo = Double.Parse(xmlNodeList[7].InnerText);
                        }
                        catch (System.Exception ex)
                        {
                            duLieuThoDTO.KinhDo = 0;
                        }

                        try
                        {
                            duLieuThoDTO.ViDo = Double.Parse(xmlNodeList[8].InnerText);
                        }
                        catch (System.Exception ex)
                        {
                            duLieuThoDTO.ViDo = 0;
                        }

                        duLieuThoDTO.ChuThich = xmlNodeList[9].InnerText;
                    }

                    lstDuLieuThoDTO.Add(duLieuThoDTO);
                }
            }
            catch (Exception ex)
            {
                lstDuLieuThoDTO = new List<DuLieuThoDTO>();
                throw ex;
            }

            return lstDuLieuThoDTO;
        }

        public List<string> GetFilterInfo(List<DuLieuDTO> lstDuLieuDTO, int type)
        {
            List<string> lstInfo = new List<string>();
            try
            {
                if (type == FILTER_INFO_DUONG)
                {
                    for (int i = 0; i < lstDuLieuDTO.Count; i++)
                    {
                        if (DataProvider._defaultNumber != lstDuLieuDTO[i].MaDuong)
                        {
                            lstInfo.Add(this._slstDuongDTO[lstDuLieuDTO[i].MaDuong]);
                        }
                    }
                    lstInfo.Sort();
                    lstInfo.Insert(0, TAT_CA_DUONG);
                }
                else if (type == FILTER_INFO_DICHVU)
                {
                    for (int i = 0; i < lstDuLieuDTO.Count; i++)
                    {
                        if (DataProvider._defaultNumber != lstDuLieuDTO[i].MaDichVu)
                        {
                            lstInfo.Add(this._slstDichVuDTO[lstDuLieuDTO[i].MaDichVu]);
                        }
                    }
                    lstInfo.Sort();
                    lstInfo.Insert(0, TAT_CA_DICHVU);
                }

            }
            catch (Exception ex)
            {
                lstInfo = new List<string>();
                throw ex;
            }

            return lstInfo.Distinct().ToList();
        }

        private int CheckFilterOption(string tenDuong, string tenDichVu)
        {
            if (string.Compare(tenDuong, TAT_CA_DUONG) != 0)
            {
                if (string.Compare(tenDichVu, TAT_CA_DICHVU) != 0)
                    return FILTER_BY_BOTH;
                else return FILTER_BY_DUONG_ONLY;
            }
            else
            {
                if (string.Compare(tenDichVu, TAT_CA_DICHVU) != 0)
                    return FILTER_BY_DICHVU_ONLY;
                else return FILTER_BY_NONE;
            }
        }

        public List<DuLieuDTO> GetFilteredData(string tenDuong, string tenPhuong, List<DuLieuDTO> lstDuLieuDTO)
        {
            DataProvider.Connect();

            int maDuong = this._duongDAO.GetIdByName(tenDuong);
            int maDichVu = this._dichVuDAO.GetIdByName(tenPhuong);

            DataProvider.Disconnect();

            List<DuLieuDTO> lstDuLieuDTOFiltered = new List<DuLieuDTO>();
            int filterOption = CheckFilterOption(tenDuong, tenPhuong);
            switch (filterOption)
            {
                case FILTER_BY_BOTH:
                    {
                        for (int i = 0; i < lstDuLieuDTO.Count; i++)
                        {
                            if (maDuong == lstDuLieuDTO[i].MaDuong)
                                if (maDichVu == lstDuLieuDTO[i].MaPhuong)
                                        lstDuLieuDTOFiltered.Add(lstDuLieuDTO[i]);
                        }
                        break;
                    }
                case FILTER_BY_DUONG_ONLY:
                    {
                        for (int i = 0; i < lstDuLieuDTO.Count; i++)
                        {
                            if (maDuong == lstDuLieuDTO[i].MaDuong)
                                lstDuLieuDTOFiltered.Add(lstDuLieuDTO[i]);
                        }
                        break;
                    }
                case FILTER_BY_DICHVU_ONLY:
                    {
                        for (int i = 0; i < lstDuLieuDTO.Count; i++)
                        {
                            if (maDichVu == lstDuLieuDTO[i].MaDichVu)
                                lstDuLieuDTOFiltered.Add(lstDuLieuDTO[i]);
                        }
                        break;
                    }
                case FILTER_BY_NONE:
                    {
                        lstDuLieuDTOFiltered = lstDuLieuDTO;
                        break;
                    }
            }

            return lstDuLieuDTOFiltered;
        }
    }
}
